package common.dao.postgre;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import org.postgresql.Driver;

/**
 * Created by common on 2017/3/30.
 */
public class PostGreJDBCDao {

    static final Logger logger = LoggerFactory.getLogger(PostGreJDBCDao.class);

    private Connection conn = null;



    public PostGreJDBCDao(){

        try
        {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://192.168.1.106:5432/yagodb";
            try
            {
                this.conn = DriverManager.getConnection(url, "root", "pkulky201");
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }



    }




    public Connection getConn(){

        return this.conn;

    }



    /*
    *******************************************
     */
    public static void testQuery(){

        PostGreJDBCDao dao = new PostGreJDBCDao();

        Connection conn = dao.getConn();

        String sql = "select * from yagofacts where predicate != '<hasGloss>' limit 10;";

        Statement stmt=null;
        ResultSet rs=null;
        try
        {
            stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);
            while(rs.next()){
                System.out.println( "( s : " + rs.getString(2) + " , p : " + rs.getString(3)  + " , o : " + rs.getString(4) + " ) ");
            }

            rs.close();
            stmt.close();
            conn.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }


    }




    public static void main(String[] args){

        System.out.println("\n\nQuery Postgre\n\n");

        testQuery();


        System.out.println("\n\nQuery Finished\n\n");

    }



}
